<?php
/**
 * Bdat Site development
 * @author Will Skates
 */

/*----------------------------------------------------------------------------------------------------*/

/**
 * Outline of properties and functionality required to make and use content in the system which is used 
 * to populate masonry blocks. In this showcase item blocks.
 * @author Will Skates
 */
require_once('aggregatedcontent.php');

class Showcase extends AggregatedContent {
	
	public $image;
	
	//Define where we can find showcase data in the database.
	public $ctable = 'showcase';
	
	
	/**
	 * (non-PHPdoc) (Note: this helps get image data for this specific data also).
	 * @see BlockModel::insert()
	 */
	public function insert()
	{
		$this->fetchImage($this->content);
		parent::insert();
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Use regex to find the location of an image url from a string of text.
	 * @param	String	$str	- The string of text in which to search.
	 * @return 	Void
	 */
	private function fetchImage($str) {
		
		$this->load->helper('curl');
		
		$imgs;
		
		preg_match_all("/<(img|IMG).*?(SRC|src)=\"(.*?)\"/",$str,$imgs);
			
		if(sizeof($imgs[3]) > 0) {
			$image = $this->fixEnteredText($imgs[3][0]);
		}
		
		if($image != "" || !is_null($image)) {
			
			//Check that this url is valid.
			if($this->isImageUrl($image) == false) {
				$this->image = "";
			}
			
			//Check that a this url actually exists. (see curl helper.)
			if(fileExistsAtUrl($image) == false) {
				$this->image = "";
			}
		}
		
		
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Parse any html that is in the image url to make sure that there are no additional scripts being passed with it.
	 * @param 	String	$str	- The url to check.
	 * @return 	String	$str	- The parsed text.
	 */
	
	private function fixEnteredText($str) {
		
		return htmlspecialchars(stripslashes(trim($str)),ENT_QUOTES);
		
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Check to make sure that there is an image extension in the url that has been found.
	 * @param	String	$str	- The url to the image.
	 */
	private function isImageUrl($str) {
		
		$types = array("jpg","jpeg","png","gif");
		return in_array( end(explode('.',$str)), $types  );
	
	}
	
}